< previous page page_207 next page >

Page 207
Solution 18
DT, Phone Home
DT and I recovered quickly. it wasn't poison after all. Just brain freeze added to the story for dramatic effect. And it turned out that a quick brain freeze was just what I needed.
''It's in the packing." I showed DT.
Packing describes how variables are aligned within a structure. Most API structures have a packing of one byte, which means that each variable in the structure appears immediately after its predecessor in memory. Four-byte packing forces variables to start either on the smaller of the variable size or on the next 32-bit boundary. That means that a single byte variable can start anywhere, a 16-bit variable must start on an even byte, a 32-bit variable must start on a 32-bit boundary, and structures as a whole start on 32-bit boundaries. Visual Basic uses 4-byte packing internally, but converts structures to 1-byte packing during API function calls. The 4-byte packing required in this case has no effect on the szEntryName string, because not only does it already appear on a 32-bit boundary after the 32-bit dwSize parameter, but it is also made up of single-byte items, which can start at any address. However, it turns out that the extra padding is added even at the end of a structure so that the next structure would start at the 32-bit boundary. That's why an invalid structure size error was returnedthe API function expects the structure to be padded to the next 32-bit boundary. It needs to be 264 bytes long, not 261. The easiest way to solve this problem is to change the structure declaration to this:
Public Type RASENTRYNAME
    dwSize As Long
    szEntryName As String * RAS_MaxEntryNameBuffer
    Padding(2) As Byte
End Type
"That's great!" exclaimed DT. "I'm not getting an error 632 anymore. Just an error 603 that indicates a larger buffer is needed."
I looked again at the code. After an error 603 occurs, the program calculates the number of entries it needs and resizes the RasEntryBuffer array as necessary:
7017a6ead0e3c4111b47a554df321e9f.gif
Res = RasEnumEntries(0, vbNullString, RasEntryBuffer(0), BufferSize, _
      RasEntries)
If Res = 603 Then

 
< previous page page_207 next page >